﻿using BCCommon;
using BCCommon.Enums;
using BCData.Sim.Company.JobTitle;
using BCDto.Sim.Company.JobTitle;
using BCEntity.Sim.Company.JobTitle;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BCData.Sim.Admin.StaffOrganizationMappingRecord;
using BCDto.Sim.Admin.Admin;

namespace BCService.Sim.Company.JobTitle
{
	public class JobTitleService : IJobTitleService
	{
		private readonly IRedisService redisService;
		private readonly IJobTitleData jobTitleData;
		private readonly IStaffOrganizationMappingRecordData staffOrganizationMappingRecordData;
		private readonly string key = string.Format("{0}", EntityNameConst.JobTitleEntity);

		public JobTitleService(
			IRedisService redisService,
			IJobTitleData jobTitleData,
			IStaffOrganizationMappingRecordData staffOrganizationMappingRecordData)
		{
			this.redisService = redisService;
			this.jobTitleData = jobTitleData;
			this.staffOrganizationMappingRecordData = staffOrganizationMappingRecordData;
		}

		#region 检查职位是否存在
		public bool Exists(int jobTitleId)
		{
			return this.jobTitleData.Exists(jobTitleId).GetAwaiter().GetResult();
		}
		#endregion

		#region 添加职位
		public JobTitleDto Add(JobTitleRequestDto requestDto,AdminDto adminDto)
		{
            if (this.jobTitleData.Exists(requestDto.JobTitleNo).GetAwaiter().GetResult())
            {
				throw new ArgumentException("此职位编号已存在!");
            }
			var entity = requestDto.As<JobTitleEntity>();
			entity.CreateAdminName = adminDto.FullName;
			entity.CreateAdminId = adminDto.AdminId;

			var result = this.jobTitleData.Add(entity).GetAwaiter().GetResult().As<JobTitleDto>();
			this.redisService.DeleteMultipleKey(key, false);
			return result;
		}
		#endregion

		#region 修改职位
		public JobTitleDto Update(JobTitlePutDto putDto,AdminDto adminDto)
		{
			if (!this.Exists(putDto.JobTitleId))
			{
				throw new ArgumentException("此记录不存在!");
			}
			if (!this.jobTitleData.Exists(putDto.JobTitleNo).GetAwaiter().GetResult())
			{
				throw new ArgumentException("此职位编号不存在!");
			}
			var entity = putDto.As<JobTitleEntity>();
			entity.EditAdminName = adminDto.FullName;
			entity.EditAdminId = adminDto.AdminId;
			var result = this.jobTitleData.Update(entity).GetAwaiter().GetResult().As<JobTitleDto>();
			this.redisService.DeleteMultipleKey(key, false);
			return result;
		}
		#endregion

		#region 删除职位
		public bool Delete(int jobTitleId)
		{
			if (!this.Exists(jobTitleId))
			{
				throw new ArgumentException("此记录不存在!");
			}
			var jobTitle = this.Get(jobTitleId);
			if (this.staffOrganizationMappingRecordData.InUse(jobTitle.JobTitleNo).GetAwaiter().GetResult())
			{
				throw new ArgumentException("此职位已被使用，无法删除!");
			}
			var result = this.jobTitleData.Delete(jobTitleId).GetAwaiter().GetResult();
			this.redisService.DeleteMultipleKey(key, false);
			return result;
		}
		#endregion

		#region 获取职位
		public JobTitleDto Get(int jobTitleId)
		{
			string cacheKey = this.redisService.GetKey(key, jobTitleId);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				var data = this.jobTitleData.GetEntity(jobTitleId).GetAwaiter().GetResult();
				return data;
			}, TimeSpan.FromDays(1));
			return result.As<JobTitleDto>();
		}
		#endregion

		#region 获取职位列表
		public IEnumerable<JobTitleDto> GetList()
		{
			var result = this.redisService.TryGet(key, () =>
			{
				return this.jobTitleData.GetList().GetAwaiter().GetResult();
			}, TimeSpan.FromDays(1));
			return result.As<IEnumerable<JobTitleDto>>();
		}
		#endregion

		#region 获取职位分页
		public PageableList<JobTitleDto> Query(string jobTitle,bool? enabled, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			string cacheKey = this.redisService.GetKey(key,jobTitle, enabled, start, end, sortName, ascending, pageIndex, pageSize);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				var data = this.jobTitleData.Query(jobTitle,enabled, start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
				return new PageableList<JobTitleDto>
				{
					Count = data.Item2,
					Items = data.Item1.As<IEnumerable<JobTitleDto>>(),
					PageIndex = pageIndex,
					PageSize = pageSize,
					ServerTime = DateTime.Now
				};
			}, TimeSpan.FromMinutes(30));

			return result;
		}
		#endregion

		#region 启用职位
		public bool Enabled(int jobTitleId)
		{
			if (!this.Exists(jobTitleId))
			{
				throw new ArgumentException("此记录不存在!");
			}
			var result = this.jobTitleData.Enabled(jobTitleId).GetAwaiter().GetResult();
			string cacheKey = this.redisService.GetKey(key, jobTitleId);
			this.redisService.DeleteAsync(cacheKey);
			return result;
		}
		#endregion

		#region 禁用职位
		public bool Disabled(int jobTitleId)
		{
			if (!this.Exists(jobTitleId))
			{
				throw new ArgumentException("此记录不存在!");
			}
			var result = this.jobTitleData.Disabled(jobTitleId).GetAwaiter().GetResult();
			string cacheKey = this.redisService.GetKey(key, jobTitleId);
			this.redisService.DeleteAsync(cacheKey);
			return result;
		}
		#endregion
	}
}